package com.archer.utils.wechatPay;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

/**
 * Http(s)请求工具类。包括get/post请求。
 *
 * @author luohuida
 * @date 2015/05/19
 */
public class HttpsRequest {

	/**
	 * 向指定URL发送POST请求
	 *
	 * @param url 请求地址
	 * @param xml 发送的XML格式数据
	 * @return String 请求地址返回的响应数据
	 */
	public static String sendPost(String url, String xml) {
		try {
			URL unencodedURL = new URL(url);
			HttpURLConnection conn = (HttpURLConnection) unencodedURL.openConnection();

			conn.setConnectTimeout(3000);
			conn.setReadTimeout(3000);
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setUseCaches(false);
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Length", xml.length() + "");

			OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
			out.write(xml);
			out.flush();
			out.close();

			String result = getOut(conn);
			return result == null ? null : result;
		} catch (Exception e) {
			System.out.println("HttpsRequest发送POST请求出现异常！" + e);
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 向指定URL发送GET方法的请求
	 *
	 * @param url   请求地址
	 * @param param 请求参数，形式如name1=value1&name2=value2。
	 * @return String     请求地址返回的响应数据
	 */

	public static String sendGet(String url, String param) {
		String result = "";
		BufferedReader in = null;
		try {
			String urlName = url + "?" + param;
			URL realUrl = new URL(urlName);
			URLConnection conn = realUrl.openConnection();

			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");

			conn.connect();
			Map<String, List<String>> map = conn.getHeaderFields();
			for (String key : map.keySet()) {
				System.out.println(key + "--->" + map.get(key));
			}

			in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
			System.out.println("HttpsRequest发送GET请求出现异常！" + e);
			e.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return result;
	}


	/**
	 * 获取URL连接的响应数据，以String形式返回
	 *
	 * @param conn URL连接
	 * @return
	 * @throws IOException
	 */
	public static String getOut(HttpURLConnection conn) throws IOException {
		if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
			return null;
		}

		BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
		String line = null;
		StringBuffer strBuf = new StringBuffer();
		while ((line = in.readLine()) != null) {
			strBuf.append(line).append("\n");
		}
		in.close();

		return strBuf.toString().trim();
	}


}
